package com.api.gbhg.zengping.action;

import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.dao.BudgetSourceDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @author: Mr.zeng
 * @create: 2021-01-13 13:51
 * 同步预算来源
 **/
public class BudgetSourceAction implements Action {
    public static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    @Override
    public String execute(RequestInfo requestInfo) {
        LOGGER.info("==========================进入同步预算Action=====================================");
        String requestId = requestInfo.getRequestid();
        //得到明细表名
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);
        //当前流程表名-类似 formtable_main_22
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestId);
        rs.next();
        String mainid = rs.getString("id");
        String sbdw = rs.getString("sbdw");
        String ysnd = rs.getString("ysnd");
        //上年结算
        RecordSet recordSet = CommonDao.selectByDetailTable(detailTables[4].getTableDBName(), mainid);
        Map<String,Object>[] map=new HashMap[recordSet.getArray().size()];
        int i=0;
        while (recordSet.next()){
            map[i]=new HashMap();
            map[i].put("requestId",requestId);
            map[i].put("sbdw",sbdw);
            map[i].put("lcbj","1");
            map[i].put("nd",ysnd);
            map[i].put("lylx","0");

            map[i].put("ysly",recordSet.getString("ysly"));
            map[i].put("gnkm",recordSet.getString("gnkm"));
            map[i].put("bkxm",recordSet.getString("bkxm"));
            map[i].put("bmjjzc",recordSet.getString("bmjjzc"));
            double hpje = recordSet.getDouble("hpje");
            map[i].put("je",hpje);
            map[i].put("syed",hpje);
            boolean b = BudgetSourceDao.insertBudgetSource(map[i]);
            if(!b){
                LOGGER.info("============================预算来源同步失败,数据同步异常======================================");
                requestInfo.getRequestManager().setMessagecontent("同步建模表数据异常");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
            i++;
        }
        //当年结算
        RecordSet recordSet5 = CommonDao.selectByDetailTable(detailTables[5].getTableDBName(), mainid);
        Map<String,Object>[] map5=new HashMap[recordSet5.getArray().size()];
        int j=0;
        while (recordSet5.next()){
            map5[j]=new HashMap();
            map5[j].put("requestId",requestId);
            map5[j].put("sbdw",sbdw);
            map5[j].put("lcbj","1");
            map5[j].put("nd",ysnd);
            map5[j].put("lylx","1");

            map5[j].put("ysly",recordSet5.getString("ysly"));
            map5[j].put("gnkm",recordSet5.getString("gnkm"));
            map5[j].put("bkxm",recordSet5.getString("bkxm"));
            map5[j].put("bmjjzc",recordSet5.getString("bmjjzc"));
            double hpje = recordSet5.getDouble("hpje");
            map5[j].put("je",hpje);
            map5[j].put("syed",hpje);
            boolean b = BudgetSourceDao.insertBudgetSource(map5[j]);
            if(!b){
                LOGGER.info("============================预算来源同步失败,数据同步异常======================================");
                requestInfo.getRequestManager().setMessagecontent("同步建模表数据异常");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
            j++;
        }
        return SUCCESS;
    }


}
